package cn.fcq.basic.controller;

import cn.fcq.basic.dto.RegisterDto;
import cn.fcq.basic.exception.BusinessException;
import cn.fcq.basic.service.IVerifyCodeService;
import cn.fcq.basic.util.AjaxResult;
import cn.fcq.basic.util.FastdfsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 验证码接口
 *  1.图形验证码
 *  2.短信验证码
 *
 *  3.邮箱验证码
 *
 *  4.各种验证码。。。
 */
@RestController
@RequestMapping("/verifyCode")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;
    /**
     * 图形验证
     */
    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey") String key){
        try {
            String base64Str = verifyCodeService.imageCode(key);
            return AjaxResult.me().setResultObj(base64Str);
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }

    }
    /**
     * 短信验证
     */
    @PostMapping("/smsCode")
    public AjaxResult smsCode(@RequestBody RegisterDto dto){
        try {
            verifyCodeService.smsCode(dto);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("保存对象失败！"+e.getMessage());
        }
    }
}
